package com.dw.etl.core.service;

import com.dw.etl.common.util.UUIDUtil;
import  com.dw.etl.common.vo.PaginationVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.dw.etl.core.api.BaseDimtbService;
import com.dw.etl.core.mapper.BaseDimtbMapper;
import com.dw.etl.core.bean.model.BaseDimtb;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * baseDimtb服务实现类
 * Created by hozeData on 2021/06/16.
 */
@Service
@Transactional
public class BaseDimtbServiceImpl implements BaseDimtbService{

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseDimtbServiceImpl.class);

    private static Map<String,BaseDimtb> dimtbMap = new HashMap<>();

    @Autowired
    private BaseDimtbMapper baseDimtbMapper;

    @Override
    public int insert(BaseDimtb baseDimtb) {
        if(StringUtils.isEmpty(baseDimtb.getPk())){
            String uuid32 = UUIDUtil.getUUID32();
            baseDimtb.setPk(uuid32);
        }
        return baseDimtbMapper.insert(baseDimtb);
    }

    @Override
    public int insertSelective(BaseDimtb baseDimtb)  {
        if(StringUtils.isEmpty(baseDimtb.getPk())){
            String uuid32 = UUIDUtil.getUUID32();
            baseDimtb.setPk(uuid32);
        }
        return baseDimtbMapper.insertSelective(baseDimtb);
    }

    @Override
    public int deleteById(String pk)  {
        return baseDimtbMapper.deleteByPrimaryKey(pk);
    }

    @Override
    public int updateByIdSelective(BaseDimtb baseDimtb)  {

        return baseDimtbMapper.updateByPrimaryKeySelective(baseDimtb);
    }

    @Override
    public int updateById(BaseDimtb baseDimtb)  {
        return baseDimtbMapper.updateByPrimaryKey(baseDimtb);
    }

    @Override
    public BaseDimtb getById(String pk)  {
        return baseDimtbMapper.selectByPrimaryKey(pk);
    }

    @Override
    public List<BaseDimtb> findByModel(BaseDimtb baseDimtb)  {
        return baseDimtbMapper.findByModel(baseDimtb);
    }

    @Override
    public PaginationVO<BaseDimtb> findPageByModel(BaseDimtb baseDimtb, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<BaseDimtb> quesList = baseDimtbMapper.findByModel(baseDimtb);
        PageInfo<BaseDimtb> pageInfo = new PageInfo<>(quesList);
        PaginationVO<BaseDimtb> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    @Override
    public BaseDimtb getByIdFromCache(String pk) {
        BaseDimtb baseDimtb;
        if((baseDimtb = dimtbMap.get(pk))==null) {
            baseDimtb = this.getById(pk);
            dimtbMap.put(pk,baseDimtb);
        }
        return baseDimtb;
    }
}
